/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.listener;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;

import com.cea.papyrus.core.actions.delete.DeleteFromDiagramAction;
import com.cea.papyrus.core.editor.CommandStackUtils;
import com.cea.papyrus.core.editpart.IUMLObjectEditPart;
import com.cea.papyrus.core.utils.Debug;

/**
 * 
 * 
 * @author Patrick Tessier
 * @since 4 oct. 06
 * this class is used to create a listener on uml element and to call automatically editPart.refresh
 * when the uml element is modified
 */

public class UmlElementDestroyListener implements Adapter {

	/**
	 * 
	 */
	private Notifier _target;
	/**
	 * 
	 */
	protected EditPart umlEditPart;

	private static final int channel = 64000;


	/**
	 * this class create a listener on the umlElement.
	 * 
	 * @param editPart the editpart of the UMLElement
	 */
	public UmlElementDestroyListener(IUMLObjectEditPart editPart){

		this.umlEditPart=(EditPart)editPart;

	}

	/**
	 * 
	 * 
	 * @param notification 
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
	 */
	public void notifyChanged(Notification notification) {
		Debug.debug(this, ""+notification, channel);
		if (notification.getEventType() == PapyrusNotification.ELEMENT_DELETED) {
			CommandStackUtils.getCommandStack().execute(umlEditPart.getCommand(new Request(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_REQUEST)));				
		}
	}
	
	/**
	 * 
	 * 
	 * @return 
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#getTarget()
	 */
	public Notifier getTarget() {
		return this._target;
	}

	/**
	 * 
	 * 
	 * @param newTarget 
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
	 */
	public void setTarget(Notifier newTarget) {
		this._target = newTarget;
	}

	/**
	 * 
	 * 
	 * @param type 
	 * 
	 * @return 
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
	 */
	public boolean isAdapterForType(Object type) {
		return type.equals(umlEditPart.getModel().getClass());
	}

}